/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/

#pragma once

/*-----------------------------------------------------------------------------
	RS driver :
-----------------------------------------------------------------------------*/

typedef HardRef<class IRSDriver		>	IPxRSDriver		;		//	rsd

typedef HardRef<class ITXManager		>	IPxTXManager	;		//	txm
typedef HardRef<class IRTManager		>	IPxRTManager	;		//	rtm
typedef HardRef<class IVBManager		>	IPxVBManager	;		//	vbm
typedef HardRef<class IFXManager		>	IPxFXManager	;		//	fxm

typedef HardRef<class ITexture			>	IPxTexture		;
typedef HardRef<class IVertexBuffer	>	IPxVertexBuffer	;
typedef HardRef<class IEffect			>	IPxEffect		;

typedef void*	EFXHandle;

enum ERSFeature {
		RS_FEATURE_STEREO		,	
		RS_FEATURE_D3D9			,
		RS_FEATURE_NV_RAWZ		,
		RS_FEATURE_NV_INTZ		,
		RS_FEATURE_NV_NULL		,
		RS_FEATURE_MAX,
	};


enum ERSClearFlag_t {		
		RS_CLEAR_COLOR		=	0x0001,
		RS_CLEAR_DEPTH		=	0x0002,	
		RS_CLEAR_STENCIL	=	0x0004,
	};
	
enum ERSFormat_t {
		RS_FORMAT_UNKNOWN			,
		RS_FORMAT_XRGB_U8			,
		RS_FORMAT_ARGB_U8			,
		RS_FORMAT_ARGB_U10			,
		RS_FORMAT_ARGB_F32			,
		RS_FORMAT_DEPTH_READABLE	,
		RS_FORMAT_DEPTH_COMPARABLE	,
		RS_FORMAT_R_F32				,
		RS_FORMAT_MAX				,
	};

enum ERSTexType_t {
		RS_TEXTURE_UNKNOWN,
		RS_TEXTURE_1D,
		RS_TEXTURE_2D,
		RS_TEXTURE_3D,
		RS_TEXTURE_CUBE,
		RS_TEXTURE_MAX
	};

enum ERSImageFormat_t {
		RS_JPG = 0,
		RS_PNG,
		RS_TGA
	};
	
enum ERSBufferSize_t {
		RS_BUFFER_FULL,
		RS_BUFFER_HALF,
		RS_BUFFER_QUARTER,
	};	


/*-----------------------------------------------------------------------------
	Resources :
-----------------------------------------------------------------------------*/

class ITexture : public ICoreObject {
	public:
		virtual uint			GetWidth		( void ) const = 0;
		virtual uint			GetHeight		( void ) const = 0;
		virtual uint			GetDepth		( void ) const = 0;
		virtual ERSFormat_t		GetFormat		( void ) const = 0;
		virtual bool			IsRenderTarget	( void ) const = 0;
		virtual void			WriteDataRaw2D	( uint level, const void *data, uint pitch ) = 0;
		virtual void			UpdateFromImage	( const IPxImage image ) = 0;

		virtual void			WriteDataRaw3D	( const float * data) = 0;

	};


class IVertexBuffer : public ICoreObject {
	public:
		virtual void			UpdateFromMesh			( const IPxTriMesh mesh, bool update_only_verts=false ) = 0;
		virtual void			DrawTris				( uint start_tri, uint num_tris ) const = 0;
		virtual void			DrawAllTris				( void ) const = 0;
	};
	

	
class IEffect : public ICoreObject {
	public:
		virtual void		SetTechnique	( const EFXHandle tech ) = 0;
		virtual void		SetTechnique	( const char *tech ) = 0;
		virtual void		Begin			( uint &num_passes ) = 0;
		virtual void		BeginPass		( uint pass ) = 0;
		virtual void		EndPass			( void ) = 0;
		virtual void		End				( void ) = 0;
		virtual void		CommitChanges	( void ) = 0;
		
		virtual EFXHandle	GetParameterByName	( EFXHandle param, const char *name ) = 0;
		virtual EFXHandle	GetTechniqueByName	( const char *name ) = 0;
		
		//	set function using EFXHandle :
		virtual void		SetUniform			( const EFXHandle param, bool b ) = 0;
		virtual void		SetUniform			( const EFXHandle param, int i ) = 0;
		virtual void		SetUniform			( const EFXHandle param, uint u ) = 0;
		virtual void		SetUniform			( const EFXHandle param, float f ) = 0;
		virtual void		SetUniform			( const EFXHandle param, const EVector &v ) = 0;
		virtual void		SetUniform			( const EFXHandle param, const EPoint  &p ) = 0;
		virtual void		SetUniform			( const EFXHandle param, const EQuaternion &q ) = 0;
		virtual void		SetUniform			( const EFXHandle param, const EColor &c )  = 0;
		virtual void		SetUniform			( const EFXHandle param, const EMatrix &m ) = 0;
		
		virtual void		SetUniformArray		( const EFXHandle param, const EVector *v, uint count ) = 0;
		virtual void		SetUniformArray		( const EFXHandle param, const EMatrix *m, uint count ) = 0;
		
		virtual void		SetTexture			( const EFXHandle param, const IPxTexture &t ) = 0;

		//	set function using const char* :
		virtual void		SetUniform			( const char* param, bool b ) = 0;
		virtual void		SetUniform			( const char* param, int i ) = 0;
		virtual void		SetUniform			( const char* param, uint u ) = 0;
		virtual void		SetUniform			( const char* param, float f ) = 0;
		virtual void		SetUniform			( const char* param, const EVector &v ) = 0;
		virtual void		SetUniform			( const char* param, const EPoint  &p ) = 0;
		virtual void		SetUniform			( const char* param, const EQuaternion &q ) = 0;
		virtual void		SetUniform			( const char* param, const EColor  &c ) = 0;
		virtual void		SetUniform			( const char* param, const EMatrix &m ) = 0;
		
		virtual void		SetUniformArray		( const char* param, const EVector *v, uint count ) = 0;
		virtual void		SetUniformArray		( const char* param, const EMatrix *m, uint count ) = 0;
		
		virtual void		SetTexture			( const char* param, const IPxTexture &t ) = 0;
	};	

/*-----------------------------------------------------------------------------
	TX manager :
-----------------------------------------------------------------------------*/

class ITXManager : public ICoreObject {
	public:
		virtual IPxTexture	CreateTexture2D		( uint w, uint h, ERSFormat_t frmt ) = 0;
		virtual IPxTexture	CreateVolumeF4		( uint w, uint h, uint d, const EColor *float_data ) = 0;
		virtual IPxTexture	CreateTextureF4		( uint w, uint h, const EColor *float_data ) = 0;
		
		virtual IPxTexture	RegisterTexture		( const EString path ) = 0;
		virtual void		UnregisterTexture	( const EString name ) = 0;
		virtual void		ReleaseTextures		( void ) = 0;
		
		virtual void		SetTexture			( uint sampler, IPxTexture texture ) = 0;
	};

/*-----------------------------------------------------------------------------
	RT manager :
-----------------------------------------------------------------------------*/

class IRTManager : public ICoreObject {
	public:
		virtual IPxTexture	AllocRenderTarget		( ERSFormat_t format, int width, int height ) = 0;
		virtual void		ClearFrameBuffer		( uint target, const EColor &color, float depth=1, uint stencil=0 ) = 0;
		
		virtual void		SetFrameBuffer			( IPxTexture ds, IPxTexture c0, IPxTexture c1=0, IPxTexture c2=0, IPxTexture c3=0 ) = 0;
		virtual void		ResetFrameBuffer		( void ) = 0;

		virtual void		CopyColorFrameData		( IPxTexture dst, IPxTexture src, bool filter ) = 0;
		virtual void		CopyDepthFrameData		( IPxTexture dst, IPxTexture src ) = 0;
		virtual void		CopyColorToBackBuffer	( IPxTexture src, bool left, bool right, bool filter ) = 0;

		virtual void		MakeScreenshot			( EString path, ERSImageFormat_t image_format ) = 0;
		virtual void		SetStereoTarget			( bool left, bool right ) = 0;

		/*	REMOVE !!!!!! */
		//virtual void		SetTargetGBuffer		( bool clear ) = 0;
		//virtual void		SetTargetShadowMap		( bool clear ) = 0;
		//virtual void		SetTargetPostProcBuffer	( uint n, bool clear ) = 0;
		//virtual void		SetTargetBackbuffer		( void ) = 0;

		//virtual IPxTexture	GetGBufferColor			( uint n ) = 0;
		//virtual IPxTexture	GetGBufferDepthStencil	( void ) = 0;
		//virtual IPxTexture	GetShadowMap			( void ) = 0;
		//virtual void		CopyGBufferDepthToBackBuffer	( void ) = 0;
		/*	REMOVE !!!!!! */
	};


/*-----------------------------------------------------------------------------
	VB manager :
-----------------------------------------------------------------------------*/

struct EUIQuad;

class	IVBManager : public ICoreObject {
	public:
		virtual IPxVertexBuffer	CreateEmptyModel	( void ) = 0;
		virtual void			DrawLines			( uint num_lines, const ERSLine_s *lines ) = 0;
		virtual void			DrawQuads			( uint num_quads, const EUIQuad *quads ) = 0;

		virtual	void			DrawFSQuad			( void ) = 0;
	};
	
/*-----------------------------------------------------------------------------
	FX manager :
-----------------------------------------------------------------------------*/

struct EFXMacro {
		EFXMacro(void) {};
		EFXMacro(const EString &_name, const EString &_def) : name(_name), definition(_def) {}
		EString	name;
		EString	definition;
	};

class	IFXManager : public ICoreObject {
	public:
		virtual IPxEffect		CompileEffect		( const EString path, uint num_macros=0, EFXMacro *macros=NULL ) = 0;
		virtual void			ClearEffects		( void ) = 0;
	};


/*-----------------------------------------------------------------------------
	RS driver itself :
-----------------------------------------------------------------------------*/

class IRSDriver : public ICoreObject {
	public:
		virtual void			ResetState		( void ) = 0;
	
		virtual bool			CheckFeature	( ERSFeature feature ) = 0;
		virtual void			GetScreenSize	( uint &w, uint &h ) = 0;
		virtual void		*	GetWindowHandle	( void ) = 0;
		
		virtual IPxTXManager	GetTXM			( void ) = 0;
		virtual IPxRTManager	GetRTM			( void ) = 0;
		virtual IPxVBManager	GetVBM			( void ) = 0;
		virtual IPxFXManager	GetFXM			( void ) = 0;
		
		virtual bool			BeginFrame		( void ) = 0;
		virtual void			EndFrame		( void ) = 0;

		virtual void			DebugMarker		( const char *text ) = 0;
	};








